<?php

header('Content-type: text/html; charset=utf-8');

require_once dirname(__FILE__).'/../config.php';
require_once cf\Config::path.'param.php';
require_once cf\Config::path.'user.php';
require_once cf\Config::path.'image.php';
require_once cf\Config::path.'api/session.php';
require_once cf\Config::path.'api/admin.php';

if (cf\Config::use_imagemagick) {
	cf\Image::$USE_IMAGEMAGICK = true;
}

function forward($url) {
	session_write_close();
	header("Location: $url");
	exit;
}

$user = cf\User::getLoggedIn();
if (!$user && !defined('NO_AUTH')) {
	forward('login.php');
}

if (!defined('SMARTY_DIR')) {
	define('SMARTY_DIR', cf\Config::smarty_dir);
}

require_once SMARTY_DIR.'Smarty.class.php';
$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->addPluginsDir(cf\Config::path.'components/');

$root_url = substr($_SERVER['REQUEST_URI'],0,stripos($_SERVER['REQUEST_URI'],'admin')+6);
if ($user) {
	$uprofile = cf\query2array("SELECT desktop_url,hide_navigation FROM cf_users WHERE id=:id",array('id'=>$user->id()));
	if ($uprofile['desktop_url']) {
		if ($root_url == $_SERVER['REQUEST_URI'] || $root_url.'index.php' == $_SERVER['REQUEST_URI']) {
			forward($uprofile['desktop_url']);
		}
		$root_url = $uprofile['desktop_url'];
	}
	$smarty->assign('uprofile',$uprofile);
}
$smarty->assign('root_url',$root_url);

date_default_timezone_set('Europe/Moscow');

$smarty->assign('user',$user);

$smarty->assign('siteName',cf\Config::site);

$mainmenu = cf\api\admin\getCategories();
$smarty->assign('mainmenu',$mainmenu);

class MainMenu {
	static public function path($id) {
		global $mainmenu;
		$path = array();
		self::find_path($id,$mainmenu,$path);
		return array_reverse($path);
	}
	
	static public function children($id) {
		global $mainmenu;
		$item = self::find($id,$mainmenu);
		return $item ? $item['children'] : array();
	}
	
	static private function find($id, $items) {
		foreach ($items as &$item) {
			$found = $item['id']==$id ? $item : self::find($id,$item['children']);
			if ($found) {
				return $found;
			}
		}
		return false;
	}
	
	static private function find_path($id, $items, &$path) {
		foreach ($items as &$item) {
			if ($item['id'] == $id) {
				$path[] = array($item['url'],$item['name']);
				return true;
			}
			if (self::find_path($id,$item['children'],$path)) {
				$path[] = array($item['url'],$item['name']);
				return true;
			}
		}
		return false;
	}
}

$menuid = '';
$smarty->assignByRef('menuid',$menuid);


function show($template, $name)
{
	global $smarty, $lang;

	$smarty->assign('name',$name);
	$smarty->assign('tpl', "$template.tpl");
	
	$crumbs = array();
	$mid = $_REQUEST['mid'];
	if ($mid) {
		$crumbs = MainMenu::path($mid);
	} else if ($_REQUEST['referer']) {
		$referer = array();
		parse_str(parse_url($_REQUEST['referer'],PHP_URL_QUERY),$referer);
		if (array_key_exists('mid',$referer)) {
			$mid = $referer['mid'];
		}
		$crumbs = array_merge(MainMenu::path($mid),array(array('',$name)));
	}
	$smarty->assign('mid',$mid);
	$smarty->assign('crumbs',$crumbs);
	
	$menutreestates = \cf\api\session\get('mainTree');
	$smarty->assign('menutreestates',$menutreestates ? $menutreestates : array());
	
	$smarty->display('main.tpl');
}

?>